
// #include <chrono>
#include <algorithm>
#include <fstream>
#include <iostream>
// #include <thread>
#include <gtest/gtest.h>

#include <regex>
#include <string>
#include <utility>
#include <vector>

#include "mdict.h"
using namespace mdict;
using std::cout;
using std::endl;
using std::string;
using kv = std::pair<string, int>;

TEST(Mdcit, save) {
    Mdict mdx("../coca/coca-2020-60000.mdx");

    mdx.init();
    auto list = mdx.keyList();
    uint64_t count = list.size();
    cout << count << endl;
    string def;
    std::regex pattern("<div class='rank'>(\\d+)</div>");
    std::smatch matches;
    std::vector<kv> data;
    for (auto it : list) {
        def = mdx.parse_definition(it->key_word, it->record_start);
        bool m = regex_search(def, matches, pattern);
        if (m) {
            // data.emplace_back(std::pair{it->key_word, matches[1]});
            data.emplace_back(it->key_word, std::stoi(matches[1].str()));
            cout << it->key_word << ": " << matches[1] << "\n";
        }
    }
    std::sort(data.begin(), data.end(),
              [](kv a, kv b) { return a.second < b.second; });
    std::ofstream csv("rank.csv");
    for (auto i : data) {
        // csv << i.first << "," << i.second << "\n";
        csv << i.first << "\n";
    }
}

TEST(Mdict, load) {
    std::ifstream csv("rank.csv");
    std::vector<string> datas;
    string s;
    while (std::getline(csv, s)) {
        datas.emplace_back(s);
    }
    cout << datas.size() << endl;
}

int main(int argc, char** argv) {
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}
